package com.ease.concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

public abstract class ConcurrentExecutor {// extends ConcurrentTaskExecutor {

	public static final int CORE_POOL_SIZE = 10;

	public static final int MAX_POOL_SIZE = 50;

	protected int corePoolSize = CORE_POOL_SIZE;

	protected int maxPoolSize = MAX_POOL_SIZE;

	protected boolean shutdownOnCompleted = false;

	protected ThreadPoolTaskExecutor threadPoolTaskExecutor;

	// /** 是否是永久Executor,如果不是则完成后shutdown */
	// protected boolean permanentExecutor = false; 多线程调用主程序怎么办?
	public ConcurrentExecutor() {

	}

	public void init() {
		if (null == threadPoolTaskExecutor) {
			threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
			threadPoolTaskExecutor.setCorePoolSize(corePoolSize > 0 ? corePoolSize : CORE_POOL_SIZE);
			threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize > 0 ? maxPoolSize : MAX_POOL_SIZE);
			threadPoolTaskExecutor.afterPropertiesSet();
		}
		// Executor concurrentExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, 60L, TimeUnit.SECONDS, new
		// LinkedBlockingQueue<Runnable>(), Executors.defaultThreadFactory(),
		// new ThreadPoolExecutor.CallerRunsPolicy());

		// Executor concurrentExecutor = new
		// ThreadPoolExecutor(corePoolSize, maxPoolSize, 60L, //
		// TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

		// this.setConcurrentExecutor(concurrentExecutor);
	}

	public void execute(Runnable task) {
		this.threadPoolTaskExecutor.execute(task);
	}

	public void execute(Runnable task, long startTimeout) {
		this.threadPoolTaskExecutor.execute(task, startTimeout);
	}

	public Future<?> submit(Runnable task) {
		return this.threadPoolTaskExecutor.submit(task);
	}

	public <T> Future<T> submit(Callable<T> task) {
		return this.threadPoolTaskExecutor.submit(task);
	}

	public void shutdown() {

		// Executor concurrentExecutor = this.getConcurrentExecutor();
		if (null != threadPoolTaskExecutor) {
			threadPoolTaskExecutor.shutdown();
		}
	}

	/**
	 * @return the shutdownOnComplete
	 */
	public boolean isShutdownOnCompleted() {
		return shutdownOnCompleted;
	}

	/**
	 * @param shutdownOnCompleted
	 *            the shutdownOnComplete to set
	 */
	public void setShutdownOnCompleted(boolean shutdownOnCompleted) {
		this.shutdownOnCompleted = shutdownOnCompleted;
	}

	/**
	 * @return the threadPoolTaskExecutor
	 */
	public ThreadPoolTaskExecutor getThreadPoolTaskExecutor() {
		return threadPoolTaskExecutor;
	}

	/**
	 * @param threadPoolTaskExecutor
	 *            the threadPoolTaskExecutor to set
	 */
	public void setThreadPoolTaskExecutor(ThreadPoolTaskExecutor threadPoolTaskExecutor) {
		this.threadPoolTaskExecutor = threadPoolTaskExecutor;
	}

	/**
	 * @return the corePoolSize
	 */
	public int getCorePoolSize() {
		return corePoolSize;
	}

	/**
	 * @param corePoolSize
	 *            the corePoolSize to set
	 */
	public void setCorePoolSize(int corePoolSize) {
		this.corePoolSize = corePoolSize;
	}

	/**
	 * @return the maxPoolSize
	 */
	public int getMaxPoolSize() {
		return maxPoolSize;
	}

	/**
	 * @param maxPoolSize
	 *            the maxPoolSize to set
	 */
	public void setMaxPoolSize(int maxPoolSize) {
		this.maxPoolSize = maxPoolSize;
	}

}
